package com.social_network.test_twitter;

import java.util.Observable;
import java.util.Observer;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test_twitter.R;

public class MainActivity extends Activity implements Observer {
	
	Twitter twitter;

    Button btlogin,btlogout, btPost,btloadFollowing, btloadFollower, btShareSMS, btShareEmail;
    TextView tvName;
    EditText edtContent,edtLink;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		
	
	
		
		final Context context = this;
		
		btlogin = (Button)findViewById(R.id.btlogin);
		btlogout = (Button)findViewById(R.id.btlogout);
		btPost = (Button)findViewById(R.id.btPost);
		edtContent = (EditText)findViewById(R.id.edtContent);
		edtLink = (EditText)findViewById(R.id.edtlink);
		tvName = (TextView)findViewById(R.id.tvName);
		
		btloadFollowing = (Button)findViewById(R.id.btloadFollowing);
		
		btloadFollower = (Button)findViewById(R.id.btloadFollower);
		
		btShareSMS = (Button)findViewById(R.id.btShareSMS);
		btShareEmail = (Button)findViewById(R.id.btShareEmail);
		
		////////////////////
		twitter = new Twitter();
		twitter.addObserver(this);
		twitter.login(context, "", "");
		
		  //==============================================================//
		 //				LogIn + LogOut									 //	
		//==============================================================//
		
		btlogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			twitter = new Twitter();
			twitter.login(context, "", "");
				tvName.setText(twitter.UserName);
			}
		});
		
	btlogout.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(twitter.LogOut())
				Toast.makeText(getBaseContext(), "Log out success", Toast.LENGTH_SHORT).show();
			else
				Toast.makeText(getBaseContext(), "Log out unsuccess", Toast.LENGTH_SHORT).show();
		}
	});
		
	  	  //==============================================================//
		 //				Post status									  	 //	
		//==============================================================//
		
		btPost.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String message = edtContent.getText().toString();
				String link = edtLink.getText().toString();
				
				if(twitter.post(message, link))
				{
					edtContent.setText("");
					edtLink.setText("");
				}
			}
		});
		
		  //==============================================================//
		 //				Get Following + Follower									 //	
		//==============================================================//
		
		btloadFollowing.setOnClickListener(new  OnClickListener() {
			
			@Override
			public void onClick(View v) {
				twitter.getFollowings();
				
			}
		} );
	
		
	
	btloadFollower.setOnClickListener(new  OnClickListener() {
			
			@Override
			public void onClick(View v) {
				twitter.getFollower();		
				
			}
		} );
	
	
	btShareSMS.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			String text = edtContent.getText().toString();
			String link = edtLink.getText().toString();
			ShareSMS(text, link);
			
		}
	});
	
	
	btShareEmail.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			String text = edtContent.getText().toString();
			String link = edtLink.getText().toString();
			ShareEmail(text, link);
		}
	});
	
	
	
	}
	
	public void ShareSMS( String message, String link){
		String content = message+" "+link;
		Intent smsIntent = new Intent(Intent.ACTION_VIEW);
		Uri uri = Uri.parse("sms:");
		smsIntent.setData(uri);
		smsIntent.putExtra("sms_body", content);
		 startActivity( smsIntent);
	}
	
	public void ShareEmail( String message, String link){
		String content = message+" "+link;
		Intent emailIntent = new Intent(Intent.ACTION_SEND);
		
		emailIntent.setType("message/rfc822") ; 
		//emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});
		emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Tittle: Share from application");
		emailIntent.putExtra(Intent.EXTRA_TEXT,content);   
			
		 startActivity(emailIntent);
	}
	
	
	//========================================================================================
	
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		JSONObject listFollow = (JSONObject) data;
		Toast.makeText(getApplicationContext(), listFollow.toString(), Toast.LENGTH_LONG).show();
		
	}

}
